package com.study.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.common.EasyUIDataGridResult;
import com.study.dao.ManagerDOMapper;
import com.study.dbo.ManagerDO;
import com.study.dbo.ManagerDOExample;
import com.study.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/*********************************************************
 * @Author ONE_LLX
 * @Description //TODO 
 * @Date 10:48 2019/10/11
 * @Class ManagerServiceImpl
 * @Version v1.0
 *********************************************************/
@Service("managerService")
public class ManagerServiceImpl implements ManagerService {


    @Autowired
    @Qualifier("managerDOMapper")
    private ManagerDOMapper mapper;

    @Transactional
    @Override
    public void insertManager(String name, String passwd) {
        ManagerDO managerDO=new ManagerDO();
        managerDO.setManagerAccount(name);
        managerDO.setManagerPasswd(passwd);
        mapper.insertSelective(managerDO);
//        System.out.println(1/0);
    }

    @Override
    public List<ManagerDO> findList() {
        ManagerDOExample example=new ManagerDOExample();
        ManagerDOExample.Criteria criteria = example.createCriteria();
        criteria.andActiveEqualTo(true);
        return mapper.selectByExample(example);
    }

    @Override
    public ManagerDO findByAccount(String name) {
        ManagerDOExample example=new ManagerDOExample();
        ManagerDOExample.Criteria criteria = example.createCriteria();
        criteria.andManagerAccountEqualTo(name);
        List<ManagerDO> managerDOS = mapper.selectByExample(example);
        if (managerDOS != null && ! managerDOS.isEmpty()){
            return managerDOS.get(0);
        }
        return null;
    }

    @Override
    public EasyUIDataGridResult findListByPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        List<ManagerDO> list = findList();
        PageInfo<ManagerDO> pageInfo=new PageInfo<>(list);
        EasyUIDataGridResult result=new EasyUIDataGridResult();
        result.setRows(list);
        result.setTotal(pageInfo.getTotal());
        return result;
    }
}
